package net.huashitong.supervision.gongjian.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.sys.util.SysUserUtils;
import net.huashitong.supervision.gongjian.dto.FeedBackDTO;
import net.huashitong.supervision.gongjian.dto.GjProjectDTO;
import net.huashitong.supervision.gongjian.entity.ProjectFeedBackEntity;
import net.huashitong.supervision.gongjian.entity.project.JungongTouchanEntity;
import net.huashitong.supervision.gongjian.entity.project.KaiGongEntity;
import net.huashitong.supervision.gongjian.entity.project.ZhengQianEntity;
import net.huashitong.supervision.gongjian.filter.JungongTouchanFilter;
import net.huashitong.supervision.gongjian.filter.KaiGongFilter;
import net.huashitong.supervision.gongjian.filter.ZhengQianFilter;
import net.huashitong.supervision.gongjian.service.GjDeptProjectService;
import net.huashitong.supervision.gongjian.service.ProjectFeedBackService;
import net.huashitong.supervision.gongjian.service.project.JungongTouchanService;
import net.huashitong.supervision.gongjian.service.project.KaiGongService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/sys/gongjian/")
@Controller
public class JungongTouchanCtrl extends JsonBaseController {
    @Autowired
    private JungongTouchanService jungongTouchanService;
    @Autowired
    private GjDeptProjectService deptProjectService;
    @Autowired
    private ProjectFeedBackService feedBackService;


    @Override
    protected String getView(String s) {
        return "/modules/task/gjtask/jungong/"+s;
    }



    @ResponseBody
    @RequestMapping(value = "/jungong",method = RequestMethod.POST)
    public JqGrid queryFilterForOwn(JungongTouchanFilter filter){
        String deptId = SysUserUtils.getDeptId();
        filter.setDeptId(deptId);
        List<JungongTouchanEntity> list = jungongTouchanService.getListFowOwn(filter);

        List<FeedBackDTO> dataList = new ArrayList<>();

        for (JungongTouchanEntity item : list) {
            FeedBackDTO dto  = new FeedBackDTO();
            BeanUtils.copyProperties(item,dto);
            dto.setProjId(item.getSysId());
            dto.setLevel(0);
            dto.setIsLeaf(true);
            dto.setExpanded(true);
            dto.setSysId(item.getSysId());
            dto.setProjectId(item.getSysId());

            List<ProjectFeedBackEntity> backList = feedBackService.getCurrentFeedBackList(item.getSysId(), 1, 4,SysUserUtils.getusername());
            if(!CollectionUtils.isEmpty(backList)){
//                dto.setIsLeaf(false);
//                    dto.setSysId(backList.get(0).getSysId());
                    dto.setProjId(item.getSysId());
                    dto.setProjectId(item.getSysId());
                    dto.setFeedbackId(backList.get(0).getSysId());
                    dto.setBackRemark(backList.get(0).getRemarks());

                    dto.setReachoals(backList.get(0).getReachoals());
                    dto.setXingxiang(backList.get(0).getXingxiang());
                    dto.setAddCurrentValue(backList.get(0).getAddCurrentValue());
                    dto.setCreateName(backList.get(0).getCreateName());
                    dto.setAudit(backList.get(0).getAudit());
                    dto.setCreateDate(backList.get(0).getCreateDate()+" "+backList.get(0).getCreateTime());
                    dto.setJungongState(backList.get(0).getJungongState());
                dto.setCompletion(backList.get(0).getCompletion());
                dto.setKaigongState(backList.get(0).getKaigongState());
                dto.setCurrentValue(backList.get(0).getCurrentValue());
                dto.setTotalValue(backList.get(0).getTotalValue());

                dataList.add(dto);



            }else {
                dataList.add(dto);
            }

        }

        return coverJqGrid(filter,dataList);
    }

    /**
     * 打开任务详情页面
     */
    @RequestMapping(value="/jungong/detail/{id}",method = RequestMethod.GET)
    public String projectDetail(@PathVariable String id, Model model) {
        System.out.println(id);

        JungongTouchanEntity entity = jungongTouchanService.getById(id);

        model.addAttribute("projectDetail",entity);


        model.addAttribute("projectId",entity.getSysId());
        //查询项目的 deptType  配合单位 1 和 责任单位2
//                projectType 1 征迁交地   2  开工提速 3 在建提速，4 竣工投产 5  招商专项行动；
       /* List<GjProjectDTO> peiheList = deptProjectService.getProjectDeptList(zhengQianEntity.getSysId(), 1,1);
        String peihes ="";
        for (GjProjectDTO item : peiheList) {
            peihes=item.getDeptName()+"、"+peihes;

        }*/
        List<GjProjectDTO> zerenList = deptProjectService.getProjectDeptList(entity.getSysId(), 2,2);
        String zerens ="";
        for (GjProjectDTO item : zerenList) {
            zerens=item.getDeptName()+"、"+zerens;

        }
//        model.addAttribute("peihe",peihes);
        model.addAttribute("zeren",zerens);


        return  getView("jungong_detail");
    }


    /**
     * 打开反馈页面
     */


    @RequestMapping(value = "/jungong/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable String id) {
        //根据自己的过滤规则过滤部门，将来可以考虑是否要修改成下拉树
       /* List<SysDept> depts = sysDeptService.query();
        SysUserDataBean datebean = sysUserService.getUserDataBeanById(id);
        model.addAttribute("datebean", datebean);
        model.addAttribute("depts", depts);*/
        System.out.println(id); //taskId
        model.addAttribute("projectDetail",new JungongTouchanEntity());
        model.addAttribute("taskId",id);

        return getView("feedbackForm");
    }


    /**
     * 管理端
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/jungong/manage",method = RequestMethod.POST)
    public JqGrid zhengqianManage(JungongTouchanFilter filter){
//        String deptId = SysUserUtils.getDeptId();
//        filter.setDeptId(deptId);
        List<JungongTouchanEntity> list = jungongTouchanService.getList(filter);

        List<FeedBackDTO> dataList = new ArrayList<>();

        for (JungongTouchanEntity item : list) {
            FeedBackDTO dto  = new FeedBackDTO();
            BeanUtils.copyProperties(item,dto);
            dto.setProjId(item.getSysId());
            dto.setLevel(0);
            dto.setIsLeaf(true);
            dto.setExpanded(true);
            dto.setSysId(item.getSysId());
            dto.setProjectId(item.getSysId());

            List<ProjectFeedBackEntity> backList = feedBackService.getCurrentFeedBackList(item.getSysId(), 1, 4,null);
            if(!CollectionUtils.isEmpty(backList)){
//                dto.setIsLeaf(false);
                if(backList.size()>1){
                    dto.setIsLeaf(false);
                    dataList.add(dto);
                    int index=1;
                    for (ProjectFeedBackEntity feedback : backList) {

                        FeedBackDTO itemDto  = new FeedBackDTO();

                        itemDto.setCurrentBack(item.getCurrentBack());
                        itemDto.setProjectName("--->>"+"("+index+")"+item.getProjectName());
                        itemDto.setLevel(1);
                        itemDto.setIsLeaf(true);
                        itemDto.setParent(item.getSysId());
                        itemDto.setExpanded(true);
                        itemDto.setSysId(feedback.getSysId());
                        itemDto.setProjId(item.getSysId());
                        itemDto.setProjectId(item.getSysId());
                        itemDto.setFeedbackId(feedback.getSysId());
                        itemDto.setBackRemark(feedback.getRemarks());
                        itemDto.setReachoals(feedback.getReachoals());
                        itemDto.setXingxiang(feedback.getXingxiang());

//                        itemDto.setAddCurrentValue(feedback.getAddCurrentValue());

                        itemDto.setCompletion(feedback.getCompletion());
                        itemDto.setKaigongState(feedback.getKaigongState());
                        itemDto.setCurrentValue(feedback.getCurrentValue());
                        itemDto.setTotalValue(feedback.getTotalValue());
                        itemDto.setJungongState(feedback.getJungongState());
                        itemDto.setCreateName(feedback.getCreateName());
                        itemDto.setAudit(feedback.getAudit());
                        dataList.add(itemDto);
                        index=index+1;
                    }
                }else {
                    dto.setSysId(backList.get(0).getSysId());
                    dto.setProjId(item.getSysId());
                    dto.setProjectId(item.getSysId());
                    dto.setFeedbackId(backList.get(0).getSysId());
                    dto.setBackRemark(backList.get(0).getRemarks());

                    dto.setReachoals(backList.get(0).getReachoals());
                    dto.setXingxiang(backList.get(0).getXingxiang());
                    dto.setAddCurrentValue(backList.get(0).getAddCurrentValue());
                    dto.setCreateName(backList.get(0).getCreateName());
                    dto.setAudit(backList.get(0).getAudit());

                    dto.setJungongState(backList.get(0).getJungongState());

                    dto.setCompletion(backList.get(0).getCompletion());
                    dto.setKaigongState(backList.get(0).getKaigongState());
                    dto.setCurrentValue(backList.get(0).getCurrentValue());
                    dto.setTotalValue(backList.get(0).getTotalValue());

                    dataList.add(dto);
                }


            }else {
                dataList.add(dto);
            }

        }

        return coverJqGrid(filter,dataList);
    }

    @RequestMapping(value="/jungong/manage/detail/{id}",method = RequestMethod.GET)
    public String showManage(@PathVariable String id, Model model) {
        System.out.println(id);

        JungongTouchanEntity entity = jungongTouchanService.getById(id);
//        ZhengQianEntity zhengQianEntity = zhengQianService.getById(id);

        model.addAttribute("projectDetail",entity);


        model.addAttribute("projectId",entity.getSysId());
        //查询项目的 deptType  配合单位 1 和 责任单位2
//                projectType 1 征迁交地   2  开工提速 3 在建提速，4 竣工投产 5  招商专项行动；
        List<GjProjectDTO> peiheList = deptProjectService.getProjectDeptList(entity.getSysId(), 1,1);
        String peihes ="";
        for (GjProjectDTO item : peiheList) {
            peihes=item.getDeptName()+"、"+peihes;

        }
        List<GjProjectDTO> zerenList = deptProjectService.getProjectDeptList(entity.getSysId(), 2,1);
        String zerens ="";
        for (GjProjectDTO item : zerenList) {
            zerens=item.getDeptName()+"、"+zerens;

        }
        model.addAttribute("peihe",peihes);
        model.addAttribute("zeren",zerens);


        return  "/modules/task/gjtask/zhongdianban/jungong_manage_detail";
    }

}
